class Solution {
public:
    int minimumDifference(vector<int>& nums, int k)
    {
        int ans = INT_MAX;
        for (int i = 0; i < nums.size(); i++)
        {
            ans = min(ans, abs(nums[i] - k));
            for (int j = i - 1; j >= 0 && (nums[j] | nums[i]) != nums[j]; j--)
            {
                nums[j] |= nums[i];
                ans = min(ans, abs(nums[j] - k));
            }
        }
        return ans == INT_MAX ? -1 : ans;
    }
};